﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using bgp8.Controls;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace bgp8
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            //give us 3x our screen resolution of play area
            DiceBoard.Width = Window.Current.Bounds.Width;
            DiceBoard.Height = Window.Current.Bounds.Height;
            DiceBoard.Background = null;
            PlayBoard.Height = Window.Current.Bounds.Height * 3;
            PlayBoard.Width = Window.Current.Bounds.Width * 3;
            Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            boardScrollViewer.MinZoomFactor = (((float)boardScrollViewer.ActualWidth) / ((float)PlayBoard.ActualWidth + 30F));
            boardScrollViewer.ZoomToFactor(boardScrollViewer.MinZoomFactor);
            DiceBoard.AddGamePiece(new Die(20));
        }

        public GamePiece ActiveSelectedPiece { get { return (GamePiece)GetValue(ActiveSelectedPieceProperty); } set { SetValue(ActiveSelectedPieceProperty, value); } }
        public DependencyProperty ActiveSelectedPieceProperty = DependencyProperty.Register("ActiveSelectedPiece", typeof(GamePiece), typeof(MainPage), null);


        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void DiceBoard_GamePieceSelectionChanged(object sender, GamePieceEventArgs e)
        {
            if (e.Piece != null) 
            {
                PlayBoard.SelectedGamePiece = null;
            }
        }

        private void PlayBoard_GamePieceSelectionChanged(object sender, GamePieceEventArgs e)
        {
            if (e.Piece != null)
            {
                DiceBoard.SelectedGamePiece = null;
            }
        }

        private void TopPanel_TopAppBarButtonClicked(object sender, GamePieceEventArgs e)
        {
            if (e.Piece.GetType().Equals(typeof(Die))) 
            {
                DiceBoard.AddGamePiece(e.Piece);
            }
            else if (e.Piece.GetType().Equals(typeof(Token))) 
            {
                PlayBoard.AddGamePiece(e.Piece);
            }
        }

    }
}
